<?php
C_base::yn_file('base'.DS.'class.func.php');
class util extends func{//杂七杂八类
	
	function __construct(){
		parent::__construct();
	}
	
	/**
	 * 判断访问类型
	 * pc or phone or weixin
	 */
	static function isMobile(){
		if(strpos(@$_SERVER['HTTP_USER_AGENT'],'MicroMessenger') !== false){
			return 'weixin';
		}
		if(isset($_SERVER['HTTP_X_WAP_PROFILE'])){//如果有HTTP_X_WAP_PROFILE则一定是移动设备
			return 'phone';
		}
		if(isset($_SERVER['HTTP_VIA'])){//如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
			return stristr($_SERVER['HTTP_VIA'],"wap") ? 'phone' : 'pc';//找不到为flase,否则为true
		}
		if(isset($_SERVER['HTTP_USER_AGENT'])){//脑残法，判断手机发送的客户端标志,兼容性有待提高
			$clientkeywords = array('nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp',
					'sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu',
					'android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi',
					'openwave','nexusone','cldc','midp','wap','mobile');
			//从HTTP_USER_AGENT中查找手机浏览器的关键字
			if(preg_match("/(".implode('|',$clientkeywords).")/i",strtolower($_SERVER['HTTP_USER_AGENT']))){
				return 'phone';
			}
		}
		if(isset($_SERVER['HTTP_ACCEPT'])){//协议法，因为有可能不准确，放到最后判断
			//如果只支持wml并且不支持html那一定是移动设备
			//如果支持wml和html但是wml在html之前则是移动设备
			if((strpos($_SERVER['HTTP_ACCEPT'],'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'],'text/html') === false || 
				(strpos($_SERVER['HTTP_ACCEPT'],'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'],'text/html')))){
				return 'phone';
			}
		}
		return 'pc';
	}
	
	/**
	 * object 转 array
	 */
	static function object_array($array){
		if(is_object($array)){
			$array = (array)$array;
		}
		if(is_array($array)){
			foreach($array as $key=>$value){
				$array[$key] = self::object_array($value);
			}
		}
		return $array;
	}
	
	/**
	 * 判断字符串长短
	 * 需要判断的字符串 $zf
	 * 判断的字符长度 $zs
	 * 不满足字符长度提示语句 $ts
	 */
	function zfpd($zf,$zs,$ts='',$xy=''){//字符判断
		$this->_is_html = 1;
		$neirong = mb_ereg_replace('^(　| )+','', $zf);
		$cun = mb_ereg_replace('(　| )+$','', $neirong);
		$nrlg=(strlen(trim($cun)) + mb_strlen(trim($cun),'UTF8'))/2;
		if($xy == '1'){
			if ($nrlg < $zs){
				if ($ts != ''){
					$this->_cuowu($ts);
				}else {
					return '-1';
				}
			}
		}else {
			if ($nrlg > $zs){
				if ($ts != ''){
					$this->_cuowu($ts);
				}else {
					return '-1';
				}
			}
		}
	}
	
	/**
	 * 上传图片
	 * $name 上传文件的name eg:$_FILES['tup']; 则sctp('tup')
	 */
	function sctp($name){
		$id = C_base::con('material.photo')->photo_save($name);
		if($id){
			$fg = explode('|', $id);
			return $fg[0];
		}else{
			return 0;
		}
	}
	
	function tpname($id){
		$lj = C_base::M('material.photo')->get_tpname($id);
		return $lj;
	}
	
	/**
	 * 上传附件
	 * $name 上传文件的name eg:$_FILES['tup']; 则sctp('tup')
	 */
	function scfj($name){
		$id = C_base::con('material.file')->file_save($name);
		if($id){
			$fg = explode('|', $id);
			return $fg[0];
		}else{
			return 0;
		}
	}
	
	/**
	 * 数组排序
	 * $rs 数组
	 * $key 数组键名
	 * $px 升序/降序（asc/desc）
	 */
	static function paixu($rs=array(),$key='id',$px='asc'){
		if($rs){
			foreach ($rs as $k=>$v){
				$keys[$k]=$v[$key];
			}
			if($px == 'asc'){//升序
				$lx = SORT_ASC;
			}else{//降序
				$lx = SORT_DESC;
			}
			array_multisort($keys,$lx,$rs);
			return $rs;
		}
	}
	
	/**
	 * 分页
	 * $rs 数组
	 * $page 当前页数
	 * $pagesize 每页显示数
	 */
	static function fenye($rs=array(),$page='1',$pagesize='10'){
		$s = ($page - 1) * $pagesize;
		$y = ($page * $pagesize) - 1;
		$t = 0;
		$rss = array();
		for($i = $s;$i<=$y;$i++){
			if(@$rs[$i]){
				$rss[$t] = $rs[$i];
				$t++;
			}
		}
		return $rss;
	}
	
	/**
	 * 删除数组中的某个键值
	 * $sz 数组
	 * $z 数组中的键名,不能去除数字键名，多个的话以,分隔
	 */
	static function quchu($sz=array(),$z=''){
		if(is_array($sz)){
			if($z != ''){
				$zh = explode(',', $z);
				$zh = array_flip($zh);//将数组中的键值互换
				$sz = array_diff_key($sz, $zh);//返回第一个数组中有,而第二个数组中没有的key的结果集。针对数组的键比较。
			}
		}
		return $sz;
	}
	
	static function unique_arr($array2D,$stkeep=false,$ndformat=true) {//二维数组去重
		// 判断是否保留一级数组键 (一级数组键可以为非数字)
		if($stkeep) $stArr = array_keys($array2D);
		// 判断是否保留二级数组键 (所有二级数组键必须相同)
		if($ndformat) $ndArr = array_keys(end($array2D));
		//降维,也可以用implode,将一维数组转换为用逗号连接的字符串
		foreach ($array2D as $v){
			$v = join(",，，",$v);
			$temp[] = $v;
		}
		//去掉重复的字符串,也就是重复的一维数组
		$temp = array_unique($temp);
		//再将拆开的数组重新组装
		foreach ($temp as $k => $v) {
			if($stkeep) $k = $stArr[$k];
			if($ndformat) {
				$tempArr = explode(",，，",$v);
				foreach($tempArr as $ndkey => $ndval) $output[$k][$ndArr[$ndkey]] = $ndval;
			}else $output[$k] = explode(",，，",$v);
		}
		return $output;
	}
	
	static function arr_one($r=array()){//二维数组转一维数组
		$rs = array();
		if($r){
			foreach ($r as $k=>$v){
				if($v){
					foreach ($v as $kk=>$vv){
						$rs[]=$vv;
					}
				}
			}
		}
		return $rs;
	}
	
	/**
	 获取用户端IP
	*/
	static function Ip(){
		if(getenv('HTTP_CLIENT_IP')) {
			$ip = getenv('HTTP_CLIENT_IP');
		} elseif(getenv('HTTP_X_FORWARDED_FOR')) {
			$ip = getenv('HTTP_X_FORWARDED_FOR');
		} elseif(getenv('REMOTE_ADDR')) {
			$ip = getenv('REMOTE_ADDR');
		} else {
			$ip = $HTTP_SERVER_VARS['REMOTE_ADDR'];
		}
		return $ip;
	}
	
	/**
	 * 生成随机数
	 * $str 范围(A代表生成字符串中包含大写字母，a代表小写字母，9代表数字)
	 * $num 生成$num位字符串,默认六位
	 */
	static function rand_sum($str='aA9',$num='6'){
		$shu = '0123456789';//数字
		$xiao = 'abcdefghijklmnopqrstuvwxyz';//小写字母
		$da = strtoupper($xiao);//大写字母
		$zfc = '';//字符串
		if(strpos($str,'a') !== false){
			$zfc .= $xiao;
		}
		if(strpos($str,'9') !== false){
			$zfc .= $shu;
		}
		if(strpos($str,'A') !== false){
			$zfc .= $da;
		}
		//$zfc = $xiao.$shu.$da;//字符串
		$count = strlen($zfc) - 1;//62-1=61
		$code = '';
		for ($i = 0; $i < $num; $i++){
			$k = rand(0, $count);//0-61之间的随机数
			$code .= $zfc[$k];//验证码
		}
		return $code;
	}
	
	static function end1($str){//获取$str的最后一个字符
		return @$str ? substr($str, -1) : '';
	}
	
	static function end2($str){//获取$str的最后两个字符
		return @$str ? substr('0'.$str, -2) : '';
	}
	
	static function md5_int($md5){//md5转整数
		$a = array("'a'","'b'","'c'","'d'","'e'","'f'","'g'","'h'","'i'","'j'","'k'","'l'","'m'","'n'","'o'","'p'","'q'","'r'","'s'","'t'","'u'","'v'","'w'","'x'","'y'","'z'");
		$b = array('0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5');
		return preg_replace($a,$b,$md5);
	}
	
	/**
	 * 加密
	 * $str 未加密字符串
	 * $key 密匙
	 */
	static function jiami($str,$key = null){
		$cipher = MCRYPT_DES; //密码类型
		$modes = MCRYPT_MODE_ECB; //密码模式
		$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher,$modes),MCRYPT_RAND);//初始化向量
		$str_encrypt = mcrypt_encrypt($cipher,$key,$str,$modes,$iv); //加密函数
		return $str_encrypt;
	}
	
	/**
	 * 解密
	 * $str 已加密字符
	 * $key 密匙
	 */
	static function jiemi($str,$key = null){
		$cipher = MCRYPT_DES; //密码类型
		$modes = MCRYPT_MODE_ECB; //密码模式
		$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher,$modes),MCRYPT_RAND);//初始化向量
		$str_decrypt = mcrypt_decrypt($cipher,$key,$str,$modes,$iv); //解密函数
		return $str_decrypt;
	}
	
	/**
	 * 发送邮件类
	 * $email 接受邮件的地址
	 * $nickname 接受人昵称
	 * $title 发送标题
	 * $content 发送内容
	 */
	static function mail_send($args=array()){
		extract($args);
		C_base::yn_file('base'.DS.'mail'.DS.'class.phpmailer.php');
		$mail = new PHPMailer();
		$mail->CharSet = 'utf-8';//字符格式
		$mail->IsSMTP();//使用SMTP发送消息
		$mail->SetLanguage('zh_cn');//设置语言
		$mail->SMTPDebug = false;//不调用集SMTP类调试
		$mail->Host		= 'smtp.qq.com';//本地地址
		$mail->SMTPAuth	= true;//使用密码认证
		$mail->Port		= 25;//端口
		$mail->Username	= 'service@rumi001.com';   //service@rumi001.com  admin
		$mail->Password	= 'EMhnrm001';   //EMhnrm001  Cocacola2
		$mail->From		= 'service@rumi001.com';
		$mail->FromName	= '思凯人力资源';
		$mail->IsHTML(true);
		$mail->AddAddress($email, $nickname);//邮件地址，收件人名称
		$mail->Subject = $title;//邮件主题
		$mail->Body = $content;//邮件内容
		return @$mail->Send();
	}
	
	/**
	 * 发送手机类
	 * $shouji 手机号
	 * $content	发送内容
	 */
	static function shouji_send($args=array()){
		extract($args);
		$name = 'hnskrz';
		$pwd = 'abcde12345';
		$content = iconv('utf-8', 'gb2312', $content);
		$fp = fopen("http://203.81.21.34/send/gsend.asp?name=$name&pwd=$pwd&dst=$shouji&msg=$content","r");
		$ret= fgetss($fp,255);
		$cc = explode('&', $ret);
		$bb = explode('=', $cc[0]);
		fclose($fp);
		return $bb[1];
	}
	
	static function preg_phone($str){//验证手机号
		if(@$str != '' && preg_match('/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/',$str)){
			return true;
		}else{
			return false;
		}
	}
	
	static function preg_email($str){//验证邮箱
		if(@$str != '' && preg_match('/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/', $str)){
			return true;
		}else{
			return false;
		}
	}
	
	static function preg_pass($str){//验证密码
		if(@$str != '' && preg_match('/^[A-Za-z0-9]+$/', $str)){
			if(strlen($str) >= 6 && strlen($str) <= 12){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
	
	static function preg_name($str){//匹配姓名
		if(@$str != '' && preg_match('/^[A-Za-z\x7f-\xff]+$/', $str)){
			return true;
		}else{
			return false;
		}
	}
	
	static function preg_sfzh($str){//验证身份证号
		if(strlen($str) != 18){//只能是18位
			return false;
		}
		$base = substr($str, 0, 17);//取出本体码
		$code = substr($str, 17, 1);//取出校验码
		$factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);//加权因子
		$codelist = array('1','0','X','9','8','7','6','5','4','3','2');//校验码对应值
		//根据前17位计算校验码
		$total = 0;
		for($i=0;$i<17;$i++){
			$total += substr($base,$i,1) * $factor[$i];
		}
		$mod = $total % 11;//取模
		if($code == $codelist[$mod]){//比较校验码
			return true;
		}else{
			return false;
		}
	}
	
	static function preg_qq($str){//验证QQ
		if(@$str != '' && preg_match('/^[1-9]*[1-9][0-9]*$/', $str)){
			return true;
		}else{
			return false;
		}
	}
	
	static function preg_yzbm($str){//验证邮政编码
		if(@$str != '' && preg_match('/^[1-9]{1}(\d+){5}$/', $str)){
			return true;
		}else{
			return false;
		}
	}
	
	static function time_age($shengri){//根据出生日期计算年龄
		$birth_year = date('Y',$shengri);
		$birth_month = date('m',$shengri);
		$birth_date = date('d',$shengri);
		$now_age = 1;//实际年龄，以出生时为1岁计
		$full_age = 0;//周岁，该变量放着，根据具体情况可以随时修改
		$now_year = date('Y',time());
		$now_date_num  = date('z',time()); //该年份中的第几天
		$birth_date_num = date('z',mktime(0,0,0,$birth_month,$birth_date,$birth_year));
		$difference = $now_date_num - $birth_date_num;
		if ($difference > 0){
			$full_age = $now_year - $birth_year;
		}else{
			$full_age = $now_year - $birth_year - 1;
		}
		$now_age = $full_age + 1;
		return $now_age;
	}
	
	static function time_date($time){//时间处理
		$cha = time() - $time;
		if($cha == 0){
			return '刚刚';
		}elseif($cha < 60){
			return $cha.'秒前';
		}elseif($cha < 3600){
			return floor($cha/60).'分钟前';
		}elseif($cha < 86400){
			return floor($cha/3600).'小时前';
		}elseif($cha < 259200){//3天内
			return floor($cha/86400).'天前';
		}else{
			return date('Y-m-d H:i:s',$time);
		}
	}
	
}